﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Generic;
using System.Data;
using Spring.Data.Common;
using System.Data.SqlClient;

namespace HotelApp
{
    public static class ImageInfoDAO
    {
        #region static field

        private static AdoTemplate template = AppConfigurer.AdoTemplate;
        private static Object lockObj = new Object();
        private static string strBase = "select Image_Code, Category_Code, Image_Name, Image_Desc, Image_Type, File_Name, File_New_Name, File_Path, Thumbnail_Path,"
            + " Creator, Create_Date, Display_Order, Is_Visible from Image_Info";

        #endregion

        #region internal methods

        internal static void CreateImageInfo(ImageInfo obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("ImageInfoDAO.CreateImageInfo: Invalid parameter");

            lock (lockObj)
            {
                //Generate ItemCode
                string strSearch = "SELECT isnull(max(Image_Code),'I000000000') FROM Image_Info ";
                string strMaxNo = template.ExecuteScalar(CommandType.Text, strSearch).ToString();
                int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
                obj.ImageCode = "I" + nNum.ToString("000000000");

                //Insert into table Image_Info
                string strCreate = "insert into Image_Info values(@Image_Code, @Category_Code, @Image_Name,"
                + "@Image_Desc, @Image_Type, @File_Name, @File_New_Name, @File_Path, @Thumbnail_Path, @Creator, @Create_Date, @Display_Order, @Is_Visible)";

                IDbParameters para = template.CreateDbParameters();
                para.Add("Image_Code", DbType.String).Value = obj.ImageCode;
                para.Add("Category_Code", DbType.String).Value = obj.CategoryCode;
                para.Add("Image_Name", DbType.String).Value = obj.ImageName;
                para.Add("Image_Desc", DbType.String).Value = obj.ImageDesc == null ? string.Empty : obj.ImageDesc;
                para.Add("Image_Type", DbType.String).Value = obj.ImageType;
                para.Add("File_Name", DbType.String).Value = obj.FileName;
                para.Add("File_New_Name", DbType.String).Value = obj.FileNewName;
                para.Add("File_Path", DbType.String).Value = obj.FilePath;
                para.Add("Thumbnail_Path", DbType.String).Value = obj.ThumbnailPath;
                para.Add("Creator", DbType.String).Value = obj.Creator;
                para.Add("Create_Date", DbType.DateTime).Value = obj.CreateDate;
                para.Add("Display_Order", DbType.Int32).Value = obj.DisplayOrder;
                para.Add("Is_Visible", DbType.String).Value = obj.IsVisible ? "Y" : "N";

                template.ExecuteNonQuery(CommandType.Text, strCreate, para);
            }
        }

        internal static void UpdateImageInfo(ImageInfo obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("ImageInfoDAO.UpdateImageInfo: Invalid parameter");

            string strSql = "update Image_Info set Image_Name=@Image_Name,"
                + " Image_Desc=@Image_Desc, Is_Visible=@Is_Visible where Image_Code=@Image_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("Image_Name", DbType.String).Value = obj.ImageName;
            para.Add("Image_Desc", DbType.String).Value = obj.ImageDesc;
            para.Add("Image_Code", DbType.String).Value = obj.ImageCode;
            para.Add("Is_Visible", DbType.String).Value = obj.IsVisible ? "Y" : "N";

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        internal static void DeleteImageInfo(string imageCode)
        {
            string strSql = "delete from Image_Info where Image_Code=@Image_Code";
            IDbParameters para = template.CreateDbParameters();
            para.Add("Image_Code", DbType.String).Value = imageCode;

            template.ExecuteNonQuery(CommandType.Text, strSql, para);
        }

        #endregion

        #region public methods

        public static ImageInfo GetImageInfo(string imageCode)
        {
            string strSql = strBase + " where Image_Code=@Image_Code";

            IDbParameters para = template.CreateDbParameters();
            para.Add("Image_Code", DbType.String).Value = imageCode;

            IList<ImageInfo> lst = template.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<ImageInfo>(MapRow), para);

            return lst.Count > 0 ? lst[0] : null;
        }

        public static IList<ImageInfo> ListImageInfos(string categoryCode, int startIndex, int limit, ref int totalCount)
        {
            IList<ImageInfo> lst = new List<ImageInfo>();

            IDbParameters para = template.CreateDbParameters();
            para.Add("CategoryCode", DbType.String).Value = categoryCode;
            para.Add("pageIndex", DbType.Int32).Value = startIndex;
            para.Add("pageSize", DbType.Int32).Value = limit;
            para.AddOut("totalCount", DbType.Int32);

            lst = template.QueryWithRowMapperDelegate(CommandType.StoredProcedure, "proc_imageinfo_paged_with_Rownumber",
                new RowMapperDelegate<ImageInfo>(MapRow), para);

            totalCount = int.Parse(((SqlParameter)(para.GetValue("totalCount"))).Value.ToString());

            return lst;
        }

        #endregion

        #region private methods

        private static ImageInfo MapRow(IDataReader dr, int rownumber)
        {
            ImageInfo obj = new ImageInfo();
            obj.ImageCode = dr.IsDBNull(0) ? "" : dr.GetString(0);
            obj.CategoryCode = dr.IsDBNull(1) ? "" : dr.GetString(1);
            obj.ImageName = dr.IsDBNull(2) ? "" : dr.GetString(2);
            obj.ImageDesc = dr.IsDBNull(3) ? "" : dr.GetString(3);
            obj.ImageType = dr.IsDBNull(4) ? "" : dr.GetString(4);
            obj.FileName = dr.IsDBNull(5) ? "" : dr.GetString(5);
            obj.FileNewName = dr.IsDBNull(6) ? "" : dr.GetString(6);
            obj.FilePath = dr.IsDBNull(7) ? "" : dr.GetString(7);
            obj.ThumbnailPath = dr.IsDBNull(8) ? "" : dr.GetString(8);
            obj.Creator = dr.IsDBNull(9) ? "" : dr.GetString(9);
            obj.CreateDate = dr.IsDBNull(10) ? DateTime.MinValue : dr.GetDateTime(10);
            obj.DisplayOrder = dr.GetInt32(11);
            obj.IsVisible = dr.GetString(12) == "Y" ? true : false;

            return obj;
        }

        #endregion
    }
}
